﻿using System;
using System.Linq;

namespace Pap.Core.Sync.Configuration
{
    public partial class SyncronizationSection
    {
        public ISyncProvider CreateLocalPeerProvider()
        {
            var type = Type.GetType(this.LocalPeer.Type);
            var providerObj =
                Activator.CreateInstance(type, this.LocalPeer.Params.OfType<Param>().Select(p => p.Value).ToArray()) as
                ISyncProvider;
            return providerObj;
        }

        public ISyncProvider CreateServerPeerProvider()
        {
            var type = Type.GetType(this.ServerPeer.Type);
            var providerObj =
                Activator.CreateInstance(type, this.ServerPeer.Params.OfType<Param>().Select(p => p.Value).ToArray()) as
                ISyncProvider;
            return providerObj;
        }

        public static SyncronizationSection FromConfig(System.Configuration.Configuration configuration)
        {
            var obj = configuration.GetSection(SyncronizationSection.SyncronizationSectionSectionName);
            return obj as SyncronizationSection;
        }
    }
}
